package com.etone.H2.engine;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.ServerSocket;
import java.net.Socket;

import com.etone.H2.client.DataSend;

public class DataServer {
	private static ServerSocket ss;
	private static Socket socket;
	private static BufferedReader in;
	private static BufferedReader in2;
	private static PrintWriter out;

	public static String Server_IP = "127.0.0.1";
	public static Integer Server_Port = 9999;
	
	public static BufferedReader getReader(){

		try {
			while(null == in || !in.ready()){
				Thread.sleep(100);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return in;
	}
	
	public DataServer() {
		try {
			ss = new ServerSocket(Server_Port);

//			while (true) {
				socket = ss.accept();
				InputStream is = socket.getInputStream();

				in = new BufferedReader(new InputStreamReader(is));
				in2 = new BufferedReader(new InputStreamReader(is));
				
				out = new PrintWriter(socket.getOutputStream(), true);
				System.out.println(in);
				
				int i = 0;
				
//				while(getReader().ready()){
//					
//					System.out.println("index :" + ++i + " " + getReader().readLine());
//				}
				
//			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		try {
			new Thread(){
				@Override
				public void run(){
					new DataServer();
				}
			}.start();
			
			new Thread(){
				@Override
				public void run(){
					new DataSend();
				}
			}.start();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		System.out.println("server & client started!");
	}
}
